package com.example.stock.service.impl;

import com.example.stock.controller.vo.StockInfoVO;
import com.example.stock.excel.*;
import com.example.stock.service.*;
import com.example.stock.util.DataUtil;
import com.example.stock.util.NumberUtil;
import jakarta.annotation.PostConstruct;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

@Service
public class StockServiceImpl implements StockService {

    MonthDataService monthDataService;

    IndustryDataService industryDataService;

    @Autowired
    WuguService wuguService;

    @Autowired
    public void setMonthDataService(MonthDataService monthDataService) {
        this.monthDataService = monthDataService;
    }

    @Autowired
    public void setIndustryDataService(@Lazy IndustryDataService industryDataService) {
        this.industryDataService = industryDataService;
    }

    @Override
    @Cacheable(value = "getStockInfo", key = "#stock")
    public StockInfoVO getStockInfo(String stock) {
        StockInfo stockInfo = getStockInfosMap().get(stock);
        if (stockInfo == null) {
            return null;
        }

        StockInfoVO vo = new StockInfoVO();
        BeanUtils.copyProperties(stockInfo, vo);

        StockIntroduce stockIntroduce = getStockIntroduce(stock);
        if (stockIntroduce != null) {
            vo.setIntroduce(stockIntroduce.getIntroduce());
            vo.setControllingShareHolder(stockIntroduce.getControllingShareHolder());
        }

        double price = monthDataService.getCurrentPrice(stock);
        if (stockInfo.getBonusRatioRmb() != null) {
            vo.setDividendYield(NumberUtil.floor(stockInfo.getBonusRatioRmb() * 10 / price, 2));
        } else {
            vo.setDividendYield(0.0);
        }

        DividendPayoutRatio dividendPayoutRatio = getDividendPayoutRatio(stock);
        if (dividendPayoutRatio != null) {
            vo.setDividendPayoutRatio(dividendPayoutRatio.getDividendPayoutRatio());
        }

        // 最新的吾股数据
        Wugu wugu = wuguService.getRawWugu(stock);
        if (wugu != null) {
            vo.setWg(wugu.getWugu());
            vo.setIndustry(wugu.getIndustry());
        }

        // tags
        List<String> tags = industryDataService.getTagsByStock(stock);
        vo.setTags(tags);

        return vo;
    }

    @Override
    public List<String> getAllStocksName() {
        return new ArrayList<>(getStockInfosMap().keySet());
    }

    private Map<String, StockInfo> stockInfosMap;

    private Map<String, DividendPayoutRatio> dividendPayoutRatioMap;

    private Map<String, StockIntroduce> stockIntroduceMap;

    @PostConstruct
    private void init() {
        // 初始始化
        stockInfosMap = DataUtil.getStockInfos().stream().collect(Collectors.toMap(StockInfo::getStock, stockInfo -> stockInfo));

        dividendPayoutRatioMap = DataUtil.getDividendPayoutRatioList().stream().collect(Collectors.toMap(DividendPayoutRatio::getStock, item -> item));

        stockIntroduceMap = getStockIntroduceList().stream().collect(Collectors.toMap(StockIntroduce::getStock, item -> item));
    }

    @Override
    public Map<String, DividendPayoutRatio> getDividendPayoutRatioMap() {
        return dividendPayoutRatioMap;
    }

    @Override
    public Map<String, StockInfo> getStockInfosMap() {
        return stockInfosMap;
    }

    @Override
    public StockIntroduce getStockIntroduce(String stock) {
        return stockIntroduceMap.get(stock);
    }

    @Override
    public List<StockIntroduce> getStockIntroduceList() {
        return DataUtil.getStockIntroduces();
    }

    @Override
    public DividendPayoutRatio getDividendPayoutRatio(String stock) {
        return getDividendPayoutRatioMap().get(stock);
    }
}